package com.kotlinvben.controller

import com.kotlinvben.bean.Result
import com.kotlinvben.entity.Department
import com.kotlinvben.service.DepartmentService
import com.kotlinvben.utils.SecurityUtils
import com.kotlinvben.vo.DepartmentCreateVO
import com.kotlinvben.vo.DepartmentUpdateVO
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/system/dept")
class DepartmentController(
    private val departmentService: DepartmentService
) {
    @PostMapping
    fun createDepartment(@Valid @RequestBody departmentCreateVO: DepartmentCreateVO): Result<Department> {
        val username = SecurityUtils.getCurrentUsername()
        val department = Department(
            name = departmentCreateVO.name,
            remarks = departmentCreateVO.remarks,
            status = departmentCreateVO.status,
            pid = departmentCreateVO.pid,
            sort = departmentCreateVO.sort,
            createBy = username,
            modifyBy = username
        )
        return Result.success(departmentService.createDepartment(department))
    }

    @PutMapping("/{id}")
    fun updateDepartment(
        @PathVariable id: Long,
        @Valid @RequestBody updateVO: DepartmentUpdateVO
    ): Result<Department> {
        return Result.success(departmentService.updateDepartment(id, updateVO))
    }

    @DeleteMapping("/{id}")
    fun deleteDepartment(@PathVariable id: Long): Result<Unit> {
        departmentService.deleteDepartment(id)
        return Result.success()
    }

    @GetMapping("/{id}")
    fun getDepartmentById(@PathVariable id: Long): Result<Department> {
        return Result.success(departmentService.getDepartmentById(id))
    }

    @GetMapping("/list")
    fun getDepartments(): Result<List<Department>> {
        val departments = departmentService.getDepartments()
        return Result.success(departments)
    }
} 